package yyy.ab.modules.generator.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import yyy.ab.common.core.BaseController;
import yyy.ab.common.core.ResultBean;
import yyy.ab.common.exception.BadRequestException;
import yyy.ab.modules.generator.entity.ColumnInfo;
import yyy.ab.modules.generator.entity.vo.TableInfo;
import yyy.ab.modules.generator.service.GenConfigService;
import yyy.ab.modules.generator.service.GeneratorService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * @author Zheng Jie
 * @date 2019-01-02
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/code/generator")
@Api(tags = "系统：代码生成管理")
public class GeneratorController extends BaseController {

    @Value("${generator.enabled}")
    private Boolean generatorEnabled;

    private final GeneratorService generatorService;
    private final GenConfigService genConfigService;

    /**
     * 代码生成页面
     */
    @RequestMapping
    public ModelAndView jump() {
        ModelAndView view = new ModelAndView();
        view.setViewName("system/generator/index");
        return view;
    }

    /**
     * 代码预览页面
     */
    @RequestMapping("/preview")
    public ModelAndView jumpPreview() {
        ModelAndView view = new ModelAndView();
        view.setViewName("system/generator/preview");
        return view;
    }

    /**
     * 代码配置页面
     */
    @RequestMapping("/config")
    public ModelAndView jumpConfig() {
        ModelAndView view = new ModelAndView();
        view.setViewName("system/generator/config");
        return view;
    }

    @ApiOperation("查询数据库数据")
    @GetMapping(value = "/tables/all")
    public Object queryTables(){
        return generatorService.getTables();
    }

    @ApiOperation("查询数据库数据")
    @GetMapping(value = "/tables")
    public Object queryTables(@RequestParam Map<String, Object> params){
        IPage<TableInfo> page = generatorService.getTables(params);
        return jsonForTable(page);
    }

    @ApiOperation("查询字段数据")
    @GetMapping(value = "/columns")
    public Object queryColumns(@RequestParam String tableName){
        List<ColumnInfo> columnInfos = generatorService.getColumns(tableName);
        return jsonForTable(columnInfos);
    }

    @ApiOperation("保存字段数据")
    @PutMapping
    public Object save(@RequestBody List<ColumnInfo> columnInfos){
        generatorService.save(columnInfos);
        return ResultBean.success();
    }

    @ApiOperation("同步字段数据")
    @PostMapping(value = "/sync")
    public Object sync(@RequestBody List<String> tables){
        for (String table : tables) {
            generatorService.sync(generatorService.getColumns(table), generatorService.query(table));
        }
        return ResultBean.success();
    }

    @ApiOperation("生成代码")
    @PostMapping(value = "/{tableName}/{type}")
    public Object generator(@PathVariable String tableName, @PathVariable Integer type, HttpServletRequest request, HttpServletResponse response){
        if(!generatorEnabled && type == 0){
            throw new BadRequestException("此环境不允许生成代码，请选择预览或者下载查看！");
        }
        switch (type){
            // 生成代码
            case 0: generatorService.generator(genConfigService.find(tableName), generatorService.getColumns(tableName));
                break;
            // 预览
            case 1: return generatorService.preview(genConfigService.find(tableName), generatorService.getColumns(tableName));
            // 打包
            case 2: generatorService.download(genConfigService.find(tableName), generatorService.getColumns(tableName), request, response);
                break;
            default: throw new BadRequestException("没有这个选项");
        }
        return new ResponseEntity<>(HttpStatus.OK);
    }
}